package com.gzf.encrption;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class MainActivity extends AppCompatActivity {

    private EditText input, output;
    private TextView mTextView;
    private String encryptResult = null;
    private String decryptResult = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        input = (EditText) findViewById(R.id.input);
        output = (EditText) findViewById(R.id.output);
        mTextView = (TextView) findViewById(R.id.showinfo);
    }

    public void encrypt(View view) {
        String data = input.getText().toString().trim();
        String password = output.getText().toString().trim();
        switch (view.getId()) {
            case R.id.des:
                encryptResult = encrypt("DES", data, password);
                mTextView.setText(encryptResult);
                break;
            case R.id.des_f:
                decryptResult = decrypt("DES", encryptResult, password);
                mTextView.setText(decryptResult);
                break;
            case R.id.aes:
                encryptResult = encrypt("AES", data, password);
                mTextView.setText(encryptResult);
                break;
            case R.id.aes_f:
                decryptResult = decrypt("AES", encryptResult, password);
                mTextView.setText(decryptResult);
                break;
            case R.id.s_des:
                encryptResult = encrypt("DESede",data,password);
                mTextView.setText(encryptResult);
                break;
            case R.id.s_des_f:
                decryptResult = decrypt("DESede", encryptResult, password);
                mTextView.setText(decryptResult);
                break;
        }
    }

    private String decrypt(String type, String data, String password) {
        switch (type) {
            case "DES":
                //获取解密对应的类,解密引擎
                try {
                    Cipher cipher = Cipher.getInstance(type);
                    //初始化，指定密码
                    if (password.length() != 8) {
                        throw new Exception("密码长度错误");
                    }
                    Key key = new SecretKeySpec(password.getBytes(), type);
                    cipher.init(Cipher.DECRYPT_MODE, key);
                    if (!TextUtils.isEmpty(data)) {
                        byte[] decode = Base64.decode(data, Base64.DEFAULT);
                        byte[] decodeBytes = cipher.doFinal(decode);
                        return new String(decodeBytes);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case "AES":
                //获取解密对应的类,解密引擎
                try {
                    Cipher cipher = Cipher.getInstance(type);
                    //初始化，指定密码
                    if (password.length() != 16) {
                        throw new Exception("密码长度错误");
                    }
                    Key key = new SecretKeySpec(password.getBytes(), type);
                    cipher.init(Cipher.DECRYPT_MODE, key);
                    if (!TextUtils.isEmpty(data)) {
                        byte[] decode = Base64.decode(data, Base64.DEFAULT);
                        byte[] decodeBytes = cipher.doFinal(decode);
                        return new String(decodeBytes);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case "DESede":
                //获取解密对应的类,解密引擎
                try {
                    Cipher cipher = Cipher.getInstance(type);
                    //初始化，指定密码
                    if (password.length() != 24) {
                        throw new Exception("密码长度错误");
                    }
                    Key key = new SecretKeySpec(password.getBytes(), type);
                    cipher.init(Cipher.DECRYPT_MODE, key);
                    if (!TextUtils.isEmpty(data)) {
                        byte[] decode = Base64.decode(data, Base64.DEFAULT);
                        byte[] decodeBytes = cipher.doFinal(decode);
                        return new String(decodeBytes);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            default:
                break;
        }
        return null;
    }

    private String encrypt(String type, String data, String password) {
        switch (type) {
            case "DES":
                //获取加密对应的类,加密引擎
                try {
                    Cipher cipher = Cipher.getInstance(type);
                    //初始化，指定密码
                    if (password.length() != 8) {
                        throw new Exception("密码长度错误");
                    }
                    Key key = new SecretKeySpec(password.getBytes(), type);
                    cipher.init(Cipher.ENCRYPT_MODE, key);
                    if (!TextUtils.isEmpty(data)) {
                        byte[] encryptBytes = cipher.doFinal(data.getBytes());
                        byte[] encode = Base64.encode(encryptBytes, Base64.DEFAULT);
                        return new String(encode);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case "AES":
                //获取加密对应的类,加密引擎
                try {
                    Cipher cipher = Cipher.getInstance(type);
                    //初始化，指定密码
                    if (password.length() != 16) {
                        throw new Exception("密码长度错误");
                    }
                    Key key = new SecretKeySpec(password.getBytes(), type);
                    cipher.init(Cipher.ENCRYPT_MODE, key);
                    if (!TextUtils.isEmpty(data)) {
                        byte[] encryptBytes = cipher.doFinal(data.getBytes());
                        byte[] encode = Base64.encode(encryptBytes, Base64.DEFAULT);
                        return new String(encode);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "DESede":
                //获取加密对应的类,加密引擎
                try {
                    Cipher cipher = Cipher.getInstance(type);
                    //初始化，指定密码
                    if (password.length() != 24) {
                        throw new Exception("密码长度错误");
                    }
                    Key key = new SecretKeySpec(password.getBytes(), type);
                    cipher.init(Cipher.ENCRYPT_MODE, key);
                    if (!TextUtils.isEmpty(data)) {
                        byte[] encryptBytes = cipher.doFinal(data.getBytes());
                        byte[] encode = Base64.encode(encryptBytes, Base64.DEFAULT);
                        return new String(encode);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            default:
                break;
        }
        return null;
    }
}
